// ===============================================
// plan_btn_widget
//
// Create by Will on 2024/4/23 22:23
// Copyright @Will.All rights reserved.
// ===============================================

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../assets/res.dart';
import '../themes/multiple_themes_mode/app_default_style.dart';
import 'img_ex/img_ex.dart';

class PlanBtnWidget extends StatefulWidget {
  const PlanBtnWidget({
    super.key,
    required this.planTitle,
    required this.onFinish,
    this.unitPrice = 0,
    this.textInputAction,
  });

  final String planTitle;
  final double unitPrice;
  final TextInputAction? textInputAction;
  final void Function(double) onFinish;

  @override
  State<PlanBtnWidget> createState() => _PlanBtnWidgetState();
}

class _PlanBtnWidgetState extends State<PlanBtnWidget> {
  double unitPrice = 0;
  int touchTime = 1;

  late TextEditingController ctrl;
  String value = '';

  @override
  void initState() {
    super.initState();
    unitPrice = widget.unitPrice;
    value = (touchTime * widget.unitPrice).toString();
    ctrl = TextEditingController(
      text: value,
    );
  }

  void onTouch({bool minus = true}) {
    if (minus) {
      touchTime--;
      if (touchTime < 1) {
        touchTime = 1;
        return;
      }
    } else {
      touchTime++;
    }

    value = (touchTime * widget.unitPrice).toString();
    ctrl.text = value;
    widget.onFinish(double.parse(value));

    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(vertical: AppStyle.padding),
      decoration: const BoxDecoration(
        border: Border(
          bottom: BorderSide(
            color: AppStyle.borderColor, // 线条颜色
          ),
        ),
      ),
      child: Row(
        children: <Widget>[
          Expanded(
            child: Text(
              widget.planTitle,
              style: AppStyle.fontBlack.copyWith(
                fontSize: 14.sp,
              ),
            ),
          ),
          Row(
            children: <Widget>[
              ImgEx(
                url: Res.imagesUserIcCoin,
                width: 16.w,
              ),
              InkWell(
                onTap: onTouch,
                child: Container(
                  height: 25.h,
                  width: 30.w,
                  alignment: Alignment.center,
                  child: Icon(
                    CupertinoIcons.minus,
                    size: 12.w,
                  ),
                ),
              ),
              SizedBox(
                width: 43.w,
                height: 25.h,
                child: TextField(
                  controller: ctrl,
                  cursorHeight: 12,
                  strutStyle: const StrutStyle(),
                  style: AppStyle.fontBlack.copyWith(
                    fontSize: 12.sp,
                    height: 1,
                  ),
                  decoration: InputDecoration(
                    // border: OutlineInputBorder(), // 设置边框
                    filled: false,
                    contentPadding: EdgeInsets.only(
                      top: 15.h,
                      left: 1.w,
                      right: 1.w,
                    ),

                    border: OutlineInputBorder(
                      ///用来配置边框的样式
                      borderSide: BorderSide(
                        ///设置边框的颜色
                        color: AppStyle.fontBlack_333.color!,
                      ),
                      borderRadius: BorderRadius.circular(5.w),
                    ),

                    ///设置输入框可编辑时的边框样式
                    enabledBorder: OutlineInputBorder(
                      ///用来配置边框的样式
                      borderSide: BorderSide(
                        ///设置边框的颜色
                        color: AppStyle.fontBlack_333.color!,
                      ),
                      borderRadius: BorderRadius.circular(5.w),
                    ),

                    ///用来配置输入框获取焦点时的颜色
                    focusedBorder: OutlineInputBorder(
                      ///用来配置边框的样式
                      borderSide: BorderSide(
                        ///设置边框的颜色
                        color: AppStyle.fontBlack_333.color!,
                      ),
                      borderRadius: BorderRadius.circular(5.w),
                    ),
                  ),
                  textAlign: TextAlign.center,
                  textInputAction:
                      widget.textInputAction ?? TextInputAction.done,
                  keyboardType: TextInputType.number,
                  enabled: false,
                ),
              ),
              InkWell(
                onTap: () => onTouch(minus: false),
                child: Container(
                  height: 25.h,
                  width: 30.w,
                  alignment: Alignment.center,
                  child: Icon(
                    CupertinoIcons.add,
                    size: 12.w,
                  ),
                ),
              ),
            ],
          )
        ],
      ),
    );
  }
}
